package org.blogwu.learn.netty学习.chapter0.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server3 {


    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("开始监听");

        while (true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            OutputStream outputStream = socket.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            try {
                System.out.println("连接的ip：");
                String str = null;
                while ((str = bufferedReader.readLine()) != null) {
                    if (str.length() <= 0) {
                        System.out.println("ccc");
                        break;
                    }
                    System.out.println("客户端传入数据：" + str);
                }
                bufferedWriter.write("HTTP/1.1 200 OK");
                bufferedWriter.write("Content-Length: 11");
                bufferedWriter.write("Hello World");
                bufferedWriter.flush();
                outputStream.flush();
                System.out.println("服务端传输完毕！");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                bufferedWriter.close();
                outputStream.close();
                bufferedReader.close();
                inputStream.close();
            }

        }
    }
}
